//
//  AppDelegate.m
//  HiQ
//
//  Created by maxwell pang on 12-7-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //设置dock栏图标
    [NSApp setApplicationIconImage:[NSImage imageNamed:@"MacQQ logo"]];
    
    //设置状态栏图标
    _statusItem = [[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength];
    [_statusItem setImage:[NSImage imageNamed:@"remind_QQlogo_normal"]];
    [_statusItem setAlternateImage:[NSImage imageNamed:@"remind_QQlogo_mousedown"]];
    [_statusItem setHighlightMode:YES];
    [_statusItem setToolTip:@"HiQ"];
    [_statusItem setTitle:@""];
    
    NSMenu		*menu;
    menu = [[NSMenu alloc] initWithTitle:@""];
    [menu addItemWithTitle:@"Hi" action:NULL keyEquivalent:@""];
    [_statusItem setMenu:menu];
    
    //从plist中读取默认偏好设置
    NSString *path = [[NSBundle mainBundle] pathForResource:@"defaults" ofType:@"plist"];
    NSDictionary *defaultValues = [[NSDictionary alloc] initWithContentsOfFile:path];
    [[NSUserDefaults standardUserDefaults]registerDefaults:defaultValues];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    //初始化登陆界面
    loginWindowController = [[LoginWindowController alloc] initWithWindowNibName:@"LoginWindowController"];
	[loginWindowController showWindow:self];
    
    //添加badge，需要移动到消息分发函数中
//    [[NSApp dockTile] setBadgeLabel:@"99+"];
    
    //dock图标跳动，可用，但是若程序有焦点在，则不会跳的
    [NSApp requestUserAttention:NSInformationalRequest];
//    [NSApp requestUserAttention:NSCriticalRequest];

}

- (void)applicationWillTerminate:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:OnOffLine object:notification];
}

- (IBAction)OnNew:(id)sender
{
	
	NSString *QQPath = [[NSBundle mainBundle] executablePath];
	const char *cQQPath = [QQPath UTF8String];
	
	NSString *ProxyPath = [NSString stringWithFormat:@"%@/%@", [QQPath stringByDeletingLastPathComponent], @"QQStartProxy"];
	const char *cProxyPath = [ProxyPath UTF8String];
    
	if (fork() == 0)
	{
		execl(cQQPath, cProxyPath, NULL);
		exit(-1);
	}
}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
    if ([MainWindowController sharedMainWindowController] == Nil) {
        if(loginWindowController == nil)
            loginWindowController = [[LoginWindowController alloc] initWithWindowNibName:@"LoginWindowController"];
        [loginWindowController showWindow:self];
        return TRUE;
    }
    [((MainWindowController*)[MainWindowController sharedMainWindowController]).window makeKeyAndOrderFront:self];
    return TRUE;
}


@end
